package com.mDigitalHealth.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.time.DateUtils;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.text.format.Time;
import android.util.Log;

import com.mDigitalHealth.R;

public class Utils {
	private static final String TAG = "Utils";
	private Service mService;

	private static Utils instance = null;

	public static String PREFERENCE_NAME = "config";
	public static String PREFERENCE_KEY_REMEMBER_PASSWORD = "remember_password";
	public static String PREFERENCE_KEY_USER_NAME = "user_name";
	public static String PREFERENCE_KEY_PASSWORD = "password";

	// public static String COMMUNITY_URL = "http://119.62.128.125:88/mdhs/community/";
	public static String COMMUNITY_URL = "http://118.192.25.71/mobilelife/mobile/index.php";// "http://118.192.25.71/mobilelife";
	public static String MESSAGE_URL = "http://119.62.128.125:88/mdhs/msg/";
	// "http://q.t99.cn/source/7/9/9/7/7997P3UWl1GE.mp3";//
	public static String VIDEO_URL = "http://118.192.25.71/mobilelife/video/越野行走介绍.3gp";// "http://www.ooklnet.com/files/381/381489/video.mp4";

	public static int UPLOAD_HOUR = 2;
	public static int UPLOAD_MINUTE = 0;
	public static int UPLOAD_SECEND = 0;
	public static String ALARM_MANAGER_UPLOAD_ACTION = "name.bagi.levente.pedometer.upload";
	public static String ALARM_MANAGER_SAVE_INFO = "name.bagi.levente.pedometer.savainof";
	public static int PENDINGINTENT_REQUEST_CODE_UPLOAD = 1;
	public static int PENDINGINTENT_REQUEST_CODE_SAVE_INFO = 2;

	public static String BROADCAST_ACTION_RESET_VALUES = "name.bagi.levente.pedometer.resetvalues";

	public static int DAILY_TARGET_STEPS = 10000;

	public static final int INITSTEP = 7;
	public static final int CYCLESTEP = 30;

	private Utils() {}

	public static Utils getInstance() {
		if (instance == null) {
			instance = new Utils();
		}
		return instance;
	}

	public void setService(Service service) {
		mService = service;
	}

	/********** SPEAKING **********/

	private boolean mSpeak = false;

	public void ding() {}

	/********** Time **********/

	public static long currentTimeInMillis() {
		Time time = new Time();
		time.setToNow();
		return time.toMillis(false);
	}

	/**
	 * quit the application
	 * 
	 * @param context
	 */
	public static void quitApplication(Context context) {
		NotificationManager mNM = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		mNM.cancel(R.string.app_name);
		System.exit(0);
		android.os.Process.killProcess(android.os.Process.myPid());

	}

	/**
	 * whether the wifi is enable
	 * 
	 * @return
	 */
	public static boolean isWifiEnable(Context context) {
		WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
		int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
		if (mWifiManager.isWifiEnabled() && ipAddress != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * format the time
	 * 
	 * @param date
	 * @return
	 */
	public static String getFormatTime(long date) {
		if (date > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("MM dd,yyyy  HH:mm");
			String time = sdf.format(date);
			return time;
		}
		return "";

	}

	/**
	 * format the time
	 * 
	 * @param date
	 * @return
	 */
	public static String getHourStampTime(long date) {
		if (date > 0) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
			String time = sdf.format(date);
			return time;
		}
		return "";

	}

	/**
	 * 
	 * 方法说明：获取格式化系统时间
	 * 
	 * @return
	 */
	public static String getSystemTimeFormat() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		Date curDate = new Date(getSystemTiem());// 获取当前时间
		String str = formatter.format(curDate);

		return str;
	}

	/**
	 * 
	 * 方法说明：系统时间
	 * 
	 * @return
	 */
	public static long getSystemTiem() {

		return System.currentTimeMillis();

	}

	public static List<String> getLastMonth(String lastDay) {
		// String str = "2011-05-12";
		// Date d = Calendar.;
		// long sec = d.getTime() / 1000; //这里可改
		// for (int i = 0; i < 31; i++) {
		// Date vt = new Date((sec + (i * 5) * 60) * 1000); //这里可以改
		// System.out.println(DateTimeUtil.format(vt, "yyyy-MM-dd"));
		// }
		return null;
	}

	/**
	 * 
	 * 方法说明：获取n天前到现在的日期（yyyy-MM-dd）
	 * 
	 * @param n
	 *            最久取n天前日期
	 * @return
	 */
	public static List<Long> getDayListInLong(int n, String start) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();// 创建实例 默认是当前时刻

		String[] ymd = start.split("-");
		int starDay = Integer.valueOf(ymd[2]);
		int starM = Integer.valueOf(ymd[1]);
		int starY = Integer.valueOf(ymd[0]);
		cal.set(starY, starM - 1, starDay);

		List<Long> dayList = new ArrayList<Long>();
		List<String> dayl = new ArrayList<String>();
		String date;

		for (int i = 0; i < n; i++) {
			cal.set(Calendar.DAY_OF_MONTH, starDay - i);// Calendar.DAY_OF_YEAR,
														// calClone.get(Calendar.DAY_OF_YEAR) - 1
			date = (sdf.format(cal.getTime()));
			dayl.add(date);
			dayList.add(cal.getTime().getTime());
		}

		return dayList;

	}

	/**
	 * 
	 * 方法说明：获取n天前到现在的日期（yyyy-MM-dd）
	 * 
	 * @param n
	 *            最久取n天前日期
	 * @return
	 */
	public static long[] getDayListInLong2(int n, String start) {
		Date startDay = str2Date(start);

		long[] dayList = new long[n];

		long showDate = 0;
		for (int i = 0; i < n; i++) {
			// 获取时间
			showDate = DateUtils.addDays(startDay, i - n).getTime();
			dayList[i] = showDate;
			// DateUtils.getFragmentInDays(startDay, i);
		}

		return dayList;

	}

	/**
	 * 
	 * 方法说明：获取n天前到现在的日期（yyyy-MM-dd）
	 * 
	 * @param n
	 *            最久取n天前日期
	 * @return
	 */
	public static List<String> getDayListInStr(int n, String start) {
		Date startDay = str2Date(start);

		List<String> dayList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long showDate = 0;
		for (int i = 0; i < n; i++) {
			// 获取时间
			showDate = DateUtils.addDays(startDay, i - n).getTime();

			dayList.add(sdf.format(showDate));
		}

		return dayList;

	}

	/**
	 * 日期字符串转换为Date类型
	 * 
	 * @param strDay
	 * @return
	 */
	public static Date str2Date(String strDay) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date day = null;
		try {
			day = sdf.parse(strDay);
		} catch (ParseException e) {
			Log.e("Utils", "format of data is wrong");
			e.printStackTrace();

		}
		return day;

	}

	/**
	 * List转换为long[]类型
	 * 
	 * @param strDay
	 * @return
	 */
	public static long[] list2LongArray(List<String> data) {

		long[] temp = new long[data.size()];
		for (int i = 0; i < data.size(); i++) {
			temp[i] = str2Date(data.get(i)).getTime();
		}
		return temp;

	}

	public static void main(String[] args) {

		Utils.getDayListInLong2(11, "2012-01-06");

	}

	public static List<Long> getbeforeDay(int n, String start) {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

		Date test = null;
		try {
			test = format1.parse(start);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 将字符串类型转换为 Date 类型

		List<String> dayl = new ArrayList<String>();
		List<Long> dayList = new ArrayList<Long>();
		for (int i = 0; i < n; i++) {

			test.setDate(test.getDate() - 1); // 设置日期为前一天的日期
			dayList.add(test.getTime());
			dayl.add(format1.format(test.clone()));
		}

		return dayList;
	}

	/**
	 * 
	 * 方法说明：获取当前日期(默认格式 yyyy-MM-dd)
	 * 
	 * @return
	 */
	public static String getNowDate() {
		Date data = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String now = dateFormat.format(data);
		return now.toString();
	}

	/**
	 * 
	 * 方法说明：获取n天前到现在的日期（yyyy-MM-dd）
	 * 
	 * @param n
	 *            最久取n天前日期
	 * @return
	 */
	public static List<String> getDayList(int n, String start) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();// 创建实例 默认是当前时刻

		String[] ymd = start.split("-");
		int starDay = Integer.valueOf(ymd[2]);
		int starM = Integer.valueOf(ymd[1]);
		int starY = Integer.valueOf(ymd[0]);
		cal.set(starY, starM - 1, starDay);

		List<String> dayList = new ArrayList<String>();
		String date;

		for (int i = 0; i < n; i++) {
			cal.set(Calendar.DATE, starDay - i);
			date = (sdf.format(cal.getTime()));
			dayList.add(date);
		}

		return dayList;

	}

	/**
	 * 得到几天前的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static String getDateBefore(String start, int day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		int starDay = convertDay2Int(start);
		int starM = convertMonth2Int(start);
		int starY = convertYear2Int(start);
		cal.set(starY, starM - 1, starDay - day);
		return sdf.format(cal.getTime());
	}

	/**
	 * 
	 * 方法说明：把字符串列席日期转换成int类型
	 * 
	 * @param start
	 * @return
	 */
	public static int convertDay2Int(String day) {
		Calendar cal = Calendar.getInstance();
		String[] ymd = day.split("-");
		Integer.valueOf(ymd[0]);// , Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2])
		cal.set(Integer.valueOf(ymd[0]), Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2]));

		return cal.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 * 方法说明：把字符串列席日期转换成int类型
	 * 
	 * @param start
	 * @return
	 */
	public static int convertMonth2Int(String day) {
		Calendar cal = Calendar.getInstance();
		String[] ymd = day.split("-");
		Integer.valueOf(ymd[0]);// , Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2])
		cal.set(Integer.valueOf(ymd[0]), Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2]));

		return cal.get(Calendar.MONTH);
	}

	/**
	 * 
	 * 方法说明：把字符串列席日期转换成int类型
	 * 
	 * @param start
	 * @return
	 */
	public static int convertYear2Int(String day) {
		Calendar cal = Calendar.getInstance();
		String[] ymd = day.split("-");
		Integer.valueOf(ymd[0]);// , Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2])
		cal.set(Integer.valueOf(ymd[0]), Integer.valueOf(ymd[1]), Integer.valueOf(ymd[2]));

		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取用户名称
	 * 
	 * @param contest
	 * @return
	 */
	public static String getUsername(Context contest) {
		SharedPreferences mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(contest);
		String name = mSharedPreferences.getString(Utils.PREFERENCE_KEY_USER_NAME, "");
		return name;
	}

}
